package ar.com.maddes.creenciasadventistas.database;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.net.Uri;
import android.provider.BaseColumns;
import ar.com.maddes.creenciasadventistas.R;

public class MyContentProvider extends ContentProvider {

	private static final int CREENCIA = 1;
	private static final int CREENCIAS = 2;
	private static final int DOCTRINA = 3;
	private static final int DOCTRINAS = 4;

	private static final String AUTHORITY = "ar.com.scholtus.creencias";
	public static final Uri BASE_CONTENT_URI = Uri.parse("content://"
			+ AUTHORITY);

	private static final UriMatcher sURIMatcher = new UriMatcher(
			UriMatcher.NO_MATCH);

	static {
		sURIMatcher.addURI(AUTHORITY, Creencia.TABLE_NAME + "/#", CREENCIA);
		sURIMatcher.addURI(AUTHORITY, Creencia.TABLE_NAME, CREENCIAS);
		sURIMatcher.addURI(AUTHORITY, Doctrina.TABLE_NAME + "/#", DOCTRINA);
		sURIMatcher.addURI(AUTHORITY, Doctrina.TABLE_NAME, DOCTRINAS);
	}

	@Override
	public boolean onCreate() {
		// Nothing to do here...
		// (this method runs on the main UI thread!)
		return false;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		String[] titles = getContext().getResources().getStringArray(
				R.array.titles);
		String[] descriptions = getContext().getResources().getStringArray(
				R.array.descriptions);
		String[] verses = getContext().getResources().getStringArray(
				R.array.verses);
		projection = new String[] { BaseColumns._ID, Creencia.TITLE,
				Creencia.DESCRIPTION, Creencia.VERSES};
		MatrixCursor cursor = new MatrixCursor(projection);
		switch (sURIMatcher.match(uri)) {
		case CREENCIAS:
			for (int i = 0; i < titles.length; i++) {
				cursor.addRow(new Object[] { i, titles[i], descriptions[i],
						verses[i] });
			}
			break;

		case CREENCIA: {
			int id = Integer.parseInt(uri.getLastPathSegment());
			cursor.addRow(new Object[] { id, titles[id], descriptions[id],
					verses[id] });
			break;
		}
		}

		return cursor;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		// Not supported yet...
		return null;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		// Not supported yet...
		return 0;
	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		// Not supported yet...
		return 0;
	}

	@Override
	public String getType(Uri uri) {
		// Not supported yet...
		return null;
	}
}
